package com.itheima.mapper;

import com.itheima.pojo.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {


    @Insert("insert into article(title,content,cover_img,state,category_id," +
            "create_user,create_time,update_time) " +
            "values (#{title},#{content},#{coverImg},#{state},#{categoryId},#{createUser},#{createTime},#{updateTime})")
    void insertAritcle(Article article);

    @Select("select count(*) from article where title = #{title} and category_id = #{categoryId} and create_user = #{createId} ")
    int countByTitle(@Param("title") String title, @Param("categoryId") Integer categoryId, @Param("createId") Integer createUser);

    @Select("select count(*) from article where title = #{title} and category_id = #{categoryId} and create_user = #{createId} and id != #{id}")
    int countByTitleAndid(@Param("title") String title, @Param("categoryId") Integer categoryId, @Param("createId") Integer createUser,@Param("id") Integer id);

    @Update("update article " +
            "set title = #{title},content = #{content}," +
            "cover_img = #{coverImg},category_id = #{categoryId},state = #{state},update_time = #{updateTime} " +
            "where id = #{id}")
    void updateArticleById(Article article);

    @Select("select * from article where id = #{id}")
    Article getArticleById(int id);

    @Delete("delete from article where id = #{id}")
    void deleteArticle(int id);


    List<Article> findPage(@Param("categoryId") Integer categoryId, @Param("state") String state,@Param("createUserId") int createUserId);
}
